<template>
    <div>
        <div id="container"></div>
        
    </div>
</template>

<script setup>
import { onMounted, onUnmounted } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";

let map = null;

onMounted(() => {
    window._AMapSecurityConfig = {
        securityJsCode: "829ad09ae782570684d61f67a080c944",
    };
    AMapLoader.load({
        key: "b196cc6ae3600c969bd4ab4e5f434475", // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ["AMap.Scale"], //需要使用的的插件列表，如比例尺'AMap.Scale'，支持添加多个如：['...','...']
    })
        .then((AMap) => {
            map = new AMap.Map("container", {
                zoom: 11,
                center: [116.405285, 39.904989]
            });
            // 构造官方卫星、路网图层
            var satelliteLayer = new AMap.TileLayer.Satellite();
            var roadNetLayer = new AMap.TileLayer.RoadNet();

            //批量添加图层
            map.add([satelliteLayer, roadNetLayer]);

            //事件绑定
            document.querySelector("#add-satellite-layer").onclick = function () {
                map.add(satelliteLayer);
            }

            document.querySelector("#remove-satellite-layer").onclick = function () {
                map.remove(satelliteLayer);
            }

            document.querySelector("#add-roadnet-layer").onclick = function () {
                map.add(roadNetLayer);
            }

            document.querySelector("#remove-roadnet-layer").onclick = function () {
                map.remove(roadNetLayer);
            }
        })
        .catch((e) => {
            console.log(e);
        });
});

onUnmounted(() => {
    map?.destroy();
});
</script>

<style lang="scss" scoped>
#container {
    width: 800px;
    height: 800px;
}
</style>